(function($) {
	var nx = nx || {}
	//we will be using this to cache the responses from the server
	var ajaxCache = {};
	
	function log( message ) {
	}
	
	$(document).ready(
			function() {
				var cache = {}, lastXhr, select_box,nav_search_in_content,form_search,input_box;
				nav_search_in_content 	= $('#nav-search-in-content');
				select_box 				= $('#nx-category-filter');
				form_search				= $('#form-search');
				input_box				= $("#nx-search-text-input");
				
				input_box.on('focus', function() {
					var cat_name_width = nav_search_in_content.width(); 
					form_search.find('.nav-searchfield-width').first().css({paddingLeft:cat_name_width + 30 + "px"});
				})
				input_box.autocomplete({
					mustMatch: true,
					source: function( request, response ) {
						var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
						//what are we searching for
						var query_type = $(this).attr('element').attr('id');
						//the cacheterm that we use to save it in the cache
						var cachedTerm = (request.term + '' + query_type + '' + $('nx-category-filter').val() ) . toLowerCase();
						//if the data is in the cache and the data is not too long, use it
						if (ajaxCache[cachedTerm] != undefined ) {
							//map the data into a response that will be understood by the autocomplete widget
							response($.map(ajaxCache[cachedTerm].products, function(item) {
								var text = item.product_name;
								if ( !request.term || matcher.test(text) )
									return {
										label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" +$.ui.autocomplete.escapeRegex(request.term) +")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>"),
										value: text,
										option: this
									};
							}));
						}//if
						//get the data from the server
						else {
							$.ajax({
								url: vmSiteurl + "modules/mod_netbase_virtuemart_search/process.php",
								dataType: "json",
								data: {
									task: "autosuggres",
									virtuemart_category_id: $("#nx-category-filter").val(),
									q: request.term
								},
								success: function( data ) {
									//cache the data for later
									ajaxCache[cachedTerm] = data;
									//map the data into a response that will be understood by the autocomplete widget
									response( $.map( data.products, function( item ) {
										var text = item.product_name;
										if ( !request.term || matcher.test(text) )
											return {
												label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" +$.ui.autocomplete.escapeRegex(request.term) +")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>"),
												value: text,
												option: this
											};
									}));
								}
							});//ajax
						}//else
					},
					minLength: 3,
					create: function(event, ui) {
						// add class theme
						$('.ui-autocomplete').prepend('<li class="ui-menu-item" role="menuitem"><p class="ghAC_title"></p><a class="ui-corner-all" tabindex="-1"><div class="ghAC-hide">Hide eBay suggestions</div></a></li>');
				        $('.ui-autocomplete').wrap('<div class="nx"></div>');
				        
				    },
					select: function( event, ui ) {
						log( ui.item ?
							"Selected: " + ui.item.label :
							"Nothing selected, input was " + this.value);
					},
					open: function() {
						$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
					},
					close: function() {
						$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
					},
					
				});
				$( "#nx-search-text-input" ).data( "autocomplete" )._renderItem = function( ul, item ) {
			        return $( "<li></li>" )
		            .data( "item.autocomplete", item )
		            .append( 
		            		(item.product_special=="1"?'title = "Popular Products"':"")  // Title, eg "Popular Products"
		                + "<a" +(item.product_special=="1"?'title = "Popular Products"':"")
		                + ((item.classN)?" class="+item.classN:"")  // className, if one is passed in
		                + ">" + item.label + "</a>" )
		            .appendTo( ul );
				};
				
				select_box.on('click change', function() {
					nav_search_in_content.text($('option:selected',this).text().replace(/-/g,""));
					var cat_name_width = nav_search_in_content.width(); 
					form_search.find('.nav-searchfield-width').first().css({paddingLeft:cat_name_width + 30 + "px"});
				})
			})//ready

}(jQuery));